package com.qdc.datacenters.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qdc.datacenters.domain.entity.ChatMsgRuleRange;
import com.qdc.datacenters.domain.query.rule.ChatMsgRuleRangeQuery;
import com.qdc.datacenters.enums.DeleteStatusEnum;
import com.qdc.datacenters.mapper.ChatMsgRuleRangeMapper;
import com.qdc.datacenters.service.IChatMsgRuleRangeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 会话规则范围表(ChatMsgRuleRange)表服务实现类
 *
 * @author danmo
 * @since 2024-03-18 10:12:11
 */
@Service
public class ChatMsgRuleRangeServiceImpl extends BaseServiceImpl<ChatMsgRuleRangeMapper, ChatMsgRuleRange> implements IChatMsgRuleRangeService {

    @Override
    public void saveBatchByRuleId(Long ruleId, List<ChatMsgRuleRangeQuery> rangeList) {
        if (CollectionUtil.isNotEmpty(rangeList)) {
            List<ChatMsgRuleRange> scopeList = new ArrayList<>();
            for (ChatMsgRuleRangeQuery userInfo : rangeList) {
                String uuId = RandomUtil.randomString(18);
                List<ChatMsgRuleRange> scopeUserList = Optional.ofNullable(userInfo.getUserIds()).orElseGet(ArrayList::new).stream().map(userId -> {
                    ChatMsgRuleRange ruleRange = new ChatMsgRuleRange();
                    ruleRange.setGroupId(uuId);
                    ruleRange.setRuleId(ruleId);
                    ruleRange.setUserId(userId);
                    if (CollectionUtil.isNotEmpty(userInfo.getWorkCycle())) {
                        ruleRange.setWorkCycle(userInfo.getWorkCycle().stream().map(String::valueOf).collect(Collectors.joining(",")));
                    }
                    ruleRange.setBTime(userInfo.getBeginTime());
                    ruleRange.setETime(userInfo.getEndTime());
                    return ruleRange;
                }).toList();
                scopeList.addAll(scopeUserList);
            }
            saveBatch(scopeList);
        }
    }

    @Override
    public void updateBatchByRuleId(Long ruleId, List<ChatMsgRuleRangeQuery> rangeList) {
        if (CollectionUtil.isNotEmpty(rangeList)) {
            delBatchByQiIds(ListUtil.toList(ruleId));
            this.saveBatchByRuleId(ruleId, rangeList);
        }
    }

    @Override
    public Boolean delBatchByQiIds(List<Long> ruleIds) {
        ChatMsgRuleRange weQiRuleScope = new ChatMsgRuleRange();
        weQiRuleScope.setDelFlag(DeleteStatusEnum.DELETE_YES.getIndex());
        return this.update(weQiRuleScope, new LambdaQueryWrapper<ChatMsgRuleRange>().in(ChatMsgRuleRange::getRuleId, ruleIds));
    }

    @Override
    public List<ChatMsgRuleRange> getRuleRangeByRuleIds(List<Long> ruleIds) {
        return list(new LambdaQueryWrapper<ChatMsgRuleRange>()
                .in(ChatMsgRuleRange::getRuleId,ruleIds)
                .eq(ChatMsgRuleRange::getDelFlag,DeleteStatusEnum.DELETE_NO.getIndex()));
    }
}

